package tower.ai.base.chat.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import tower.ai.base.chat.service.MoonshotChatService;

@Slf4j
@RequestMapping("/moonshot")
@RestController
public class MoonshotChatController {

    @Autowired
    private MoonshotChatService chatService;

    @GetMapping("/chat")
    public ChatResponse chat(@RequestParam("message") String message,
                             @RequestParam(value="history",required = false) String history,
                             @RequestParam(value="uid",required = false) String uid) {
        return chatService.chat(message, history, uid);
    }

    @GetMapping("/streamChat")
    public Flux<String> streamChat(@RequestParam("message") String message,
                                   @RequestParam(value="history",required = false) String history,
                                   @RequestParam(value="uid",required = false) String uid) {
        Flux<ChatResponse> chatResp =  chatService.streamChat(message, history, uid);
        return chatResp.map(chatObj -> chatObj.getResult().getOutput().getContent());
    }

}
